package cn.smbms.controller;

import cn.smbms.pojo.Bill;
import cn.smbms.pojo.User;
import cn.smbms.pojo.provider;
import cn.smbms.service.BillService;
import cn.smbms.service.providerService;
import cn.smbms.util.Constants;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname BillController
 * @Description TODO
 * @Date 2020-10-11 10:47
 * @Created by 26440
 */
@Controller
public class BillController {

    @Autowired
    private providerService providerService;

    @Autowired
    private BillService billService;

    @RequestMapping("bill.do")
    public String billList(String queryProductName,
                           Integer queryProviderId,
                           Integer queryIsPayment,
                           HttpServletRequest request,
                           @RequestParam(defaultValue = "1") Integer pageIndex) {


        //条件构造器
        QueryWrapper<Bill> wrapper = new QueryWrapper<Bill>();
        wrapper.like(!StringUtils.isEmpty(queryProductName), "productName", queryProductName);
        wrapper.eq(queryProviderId != null && queryProviderId != 0, "providerId", queryProviderId);
        wrapper.eq(queryIsPayment != null && queryIsPayment != 0, "isPayment", queryIsPayment);

        System.out.println(queryProductName);
        System.out.println(queryProviderId);
        System.out.println(queryIsPayment);
        //查询供应商信息
        List<provider> providerList = providerService.list();

        //实现分页
        PageHelper.startPage(pageIndex, 6);
        List<Bill> billList = billService.getSelectBill(wrapper);


        PageInfo pi = new PageInfo(billList);
        request.setAttribute("totalPageCount", pi.getPages());
        request.setAttribute("totalCount", pi.getTotal());
        request.setAttribute("currentPageNo", pi.getPageNum());
        request.setAttribute("billList", billList);
        request.setAttribute("providerList", providerList);

        return "billlist";
    }

    /**
     * 查看某个订单信息
     *
     * @param billid
     * @param request
     * @return
     */
    @RequestMapping("billView.do/{id}")
    public String BillView(@PathVariable("id") Integer billid, HttpServletRequest request) {
        Bill bill = billService.getSelectBill(new QueryWrapper<Bill>().eq("b.id", billid)).get(0);
        request.setAttribute("bill", bill);
        return "billview";
    }

    /**
     * 删除某个订单
     */
    @RequestMapping("billDelete.do")
    @ResponseBody
    public Object billDelete(Integer billid) {
        Map<String, Object> map = new HashMap<String, Object>();

        if (billService.removeById(billid)) {
            map.put("delResult", "true");
        } else {
            map.put("delResult", "false");
        }
        return map;
    }

    /**
     * (添加)查询供应商信息
     *
     * @param request
     * @return
     */
    @RequestMapping("billAdd.do")
    public String billAdd(HttpServletRequest request) {
        List<provider> providerList = providerService.list();

        request.setAttribute("providerList", providerList);
        return "billadd";
    }

    @RequestMapping("billSave.do")
    public String billSave(@Valid Bill bill, HttpServletRequest request) {
        //获取当前用户
        User LogUser = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        //获取id
        LogUser.setCreatedBy(LogUser.getId());
        //获取当前日期
        LogUser.setCreationDate(new Date());
        bill.setCreationDate(new Date());
        if (billService.save(bill)) {
            //添加成功
            return "redirect:bill.do";
        }
        return "redirect:billadd.jsp";
    }

    /**
     * 修改订单管理信息
     *
     * @param billid
     * @param request
     * @return
     */
    @RequestMapping("billModify.do")
    public String billModify(Integer billid, HttpServletRequest request) {
        Bill bill = billService.getById(billid);
        request.setAttribute("bill", bill);
        return "billmodify";
    }

    @RequestMapping(value = "getProvider.do", method = RequestMethod.GET,
            produces = "application/json;charset-utf8")
    @ResponseBody
    public Object getProvider() {
        List<provider> providerList = providerService.list();
        return providerList;
    }

    @RequestMapping("billUpdate.do")
    public String billUpdate(Bill bill,HttpServletRequest request) {
        //拿到登入的id
        User oldUser = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        bill.setModifyBy(oldUser.getId());
        bill.setModifyDate(new Date());
        if (billService.updateById(bill)) {
            return "redirect:bill.do";
        }
        return "redirect:billModify.do?billid=" + bill.getId();
    }
}
